import axios from "axios";
import { message } from "ant-design-vue";
// import { createPinia } from 'pinia'
import { useCounterStore } from "@/pain/login/login"
// const pinia = createPinia()

import CryptoJS from "crypto-js";
import router from '@/router';

axios.defaults.headers.common["Access-Control-Allow-Origin"] = "http://localhost:8081";
// axios.defaults.headers.post["Content-Type"] ="application/x-www-form-urlencoded";
  // 'Content-Type': 'application/x-www-form-urlencoded'
const instance = axios.create({
  baseURL: 'https://www.6ocbx.com/api',
  timeout: 1000000000,
  withCredentials: false,
  headers: {"Content-Type":"application/x-www-form-urlencoded"},
});
// const instance1 = axios.create({
//   baseURL: 'https://api.6ocbx.com/',
//   timeout: 1000000000,
//   withCredentials: true,
//   headers: {},
//   headers: {},
// });
// 请求拦截器 sign t token
 //rc4加密函数
 const rc4_  =(key, str)=> {
  var S = [], K = [];
  for (var i = 0; i < 256; i++) {
      S[i] = i;
      K[i] = key.charCodeAt(i % key.length);
  }
  var j = 0;
  for (var i = 0; i < 256; i++) {
      j = (j + S[i] + K[i]) % 256;
      var temp = S[i];
      S[i] = S[j];
      S[j] = temp;
  }
  var i = 0, j = 0, res = '';
  for (var k = 0; k < str.length; k++) {
      i = (i + 1) % 256;
      j = (j + S[i]) % 256;
      var temp = S[i];
      S[i] = S[j];
      S[j] = temp;
      var t = (S[i] + S[j]) % 256;
      var key = S[t];
      res += String.fromCharCode(str.charCodeAt(k) ^ key);
  }
  return res;
}
const realToken_=ref()
const virtuallyToken_ = 'r8nuIUzW'
instance.interceptors.request.use(config => {
  let t = Math.floor(Date.now() / 1000);
                    let indexs = [];
                    for (var k in config) {
                        indexs.push(k)
                    }
                    indexs.sort()
                    let signstr = ""
                    for (var i in indexs) {
                        signstr += indexs[i] + config[indexs[i]]
                    }
                    
                    realToken_.value = rc4_('Abc123!@#&', atob(virtuallyToken_))
                    signstr = realToken_.value + signstr + realToken_.value + "t=" + t
                    signstr = CryptoJS.MD5(signstr).toString().toLocaleUpperCase()
  // 如果存在token，则在请求头中加入token
  let token = localStorage.getItem("token");
  config.headers.sign=signstr,
  config.headers.t= t,
  config.headers.token= realToken_.value
 
//   config.headers.Authorization = token ? `Bearer ${token}` : -1;
  return config;
}, error => {
  // 请求错误处理
  return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(
  function (response) {
    if (response.data instanceof Blob) return response.data;
    if (response.data.code == 90012) {
      //由于挂载时机问题，必须在回调中创建store实例
      const store = useCounterStore()
      message.warning('登录已过期，请重新登录!')
      router.push('/')
      store.vipw = true;
      store.vip = false;
      store.userLogin = true;
      store.switchVip = false;
      store.vips = false;
      store.vipMembers = false;
      store.switchVip = false;
      store.platinum = false;
      store.vips = false;
      store.diamond = false;
      return Promise.reject(error) // 终止执行并抛出错误
    }
    return response
  },
  function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

export default instance;